<?php
//TODO title of the web site, and ask if need diff titles on diff pagas
//TODO create Zend_Cache
//TODO create multilanguage


//TODO add jQuery for moving picture on the main page


class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
	 
	/**
	 * Inizialization Locale і Translatio
	 *
	 * @return void
	 */
	protected function _initDoctype() {
		$this->bootstrap ( 'layout' );
		
		$layout = $this->getResource ( 'layout' );
		$view = $layout->getView ();
		
		$view->headMeta ()->appendHttpEquiv ( 'Content Type', "text/html; charset=utf-8" );
		
		$view->headTitle ( "Великий народ" );
		
		$view->headTitle ()->setSeparator ( " :: " );
		//$view->addHelperPath('Zend/Dojo/View/Helper/', 'Zend_Dojo_View_Helper');
		
		if (! Zend_Auth::getInstance ()->hasIdentity ()) {
			$view->identity = false;
		} else {
			$view->identity = Zend_Auth::getInstance ()->getIdentity ();
		}
	}
	
	//add default empty workspace 'namespace' to work with all classes
	protected function _initAutoload() {
		//start sesionz
		Zend_Session::start ();
		
		$localLangSession = new Zend_Session_Namespace ( 'lang' );
		
		//set default language
		if (isset ( $localLang->lang )) {
			$localLangSession->lang = 'uk';
		}
		
		$localLang = $localLangSession->lang;
		 
		$translate = new Zend_Translate ( 'array', APPLICATION_PATH . '/languages', $localLang, array ('scan' => Zend_Translate::LOCALE_DIRECTORY ) );
		Zend_Registry::set ( 'Zend_Translate', $translate );
		
		//TODO cache translation
		//add cache for translation
		/*$frontendOptions = array('lifetime' => 600, 'automatic_serialization' => true);
		 $backendOptions  = array('cache_dir' => 'path_to_cache_dir');

		 $cache = Zend_Cache::factory('Core', 'File', $frontendOptions, $backendOptions);

		 Zend_Translate::setCache($cache); */
		
		$moduleloader = new Zend_Application_Module_Autoloader ( array ('namespace' => '', 'basePath' => dirname ( __FILE__ ) ) );
		$autoloader = Zend_Loader_Autoloader::getInstance ();
		$autoloader->registerNamespace ( array ('Df_', 'Gd_'));
		return $moduleloader;
	}
	
	protected function _initEmail() {
		$email_config = array 
		('auth' => 'plain', 
		'username' => 'bot', 
		'password' => 'c47GRCJW', 
		'ssl' => 'tls', 
		'port' => '25' );
		
		$transport = new Zend_Mail_Transport_Smtp ( '188.127.244.201', $email_config );
		Zend_Mail::setDefaultTransport ( $transport );
	}
	
	
//	protected function _initZFDebug()
//	{
//	    $autoloader = Zend_Loader_Autoloader::getInstance();
//	    $autoloader->registerNamespace('ZFDebug');
//	    
//	    $options = array(
//	        'plugins' => array('Variables', 
//	                           'File' => array('base_path' => '/var/www/kazantip/'),
//	                           'Memory', 
//	                           'Time', 
//	                           'Registry', 
//	                           'Exception'
//	    						)
//	    );
//	    
//	    # Instantiate the database adapter and setup the plugin.
//	    # Alternatively just add the plugin like above and rely on the autodiscovery feature.
//	    if ($this->hasPluginResource('db')) {
//	        $this->bootstrap('db');
//	        $db = $this->getPluginResource('db')->getDbAdapter();
//	        $options['plugins']['Database']['adapter'] = $db;
//	    }
//	
//	    # Setup the cache plugin
//	    if ($this->hasPluginResource('cache')) {
//	        $this->bootstrap('cache');
//	        $cache = $this-getPluginResource('cache')->getDbAdapter();
//	        $options['plugins']['Cache']['backend'] = $cache->getBackend();
//	    }
//	
//	    $debug = new ZFDebug_Controller_Plugin_Debug($options);
//	    
//	    $this->bootstrap('frontController');
//	    $frontController = $this->getResource('frontController');
//	    $frontController->registerPlugin($debug);
//	}
	
	protected function _initRouter() {
		$ctrl = Zend_Controller_Front::getInstance ();
		$router = $ctrl->getRouter (); // returns a rewrite router by default
		
		//get list of controllers, and use them directly for showing then, other show login
		//I know, that's stupid idea, but can't find other solution
		$contr = array("error","index","lang","search","user","friend","message","guests","preferences", "photo", "action", "media");
		
		foreach ($contr as $value_contr) {	
			$route_index = new Zend_Controller_Router_Route(
	    		$value_contr,
	    		array('controller' => $value_contr)
			);
			
			$router->addRoute($value_contr, $route_index);
		}
		
		//work with any char, except next static router (controller names)
		$route_login =  new Zend_Controller_Router_Route_Regex(
			    '(\w+)',
			    array(
			        'controller'    => 'index',
			        'action'        => 'main'
			    ),
			    array(
			        1 => 'login'
			    ),
			    '%w'
			);
		$router->addRoute ( 'user', $route_login );
	}
}
